package ru.ncedu.java.vlku.hrs.uil;

import ru.ncedu.java.vlku.hrs.uil.cmd.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class UserInterface {

    public void run() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        Map<String, Command> create = new HashMap<String, Command>();
        Map<String, Command> update = new HashMap<String, Command>();
        Map<String, Command> get = new HashMap<String, Command>();
        Map<String, Command> getall = new HashMap<String, Command>();
        Map<String, Command> addto = new HashMap<String, Command>();
        create.put("customer_account", new CreateCustomerAccount());
        create.put("cargo", new CreateCargo());
        create.put("shop", new CreateShop());
        create.put("storage", new CreateStorage());
        create.put("price", new CreatePrice());
        create.put("tariff", new CreateTariff());
        create.put("bill", new CreateBill());

        update.put("customer_account", new UpdateCustomerAccount());
        update.put("cargo", new UpdateCargo());
        update.put("shop", new UpdateShop());
        update.put("storage", new UpdateStorage());
        update.put("price", new UpdatePrice());
        update.put("tariff", new UpdateTariff());

        get.put("customer_account", new GetCustomerAccount());
        get.put("cargo", new GetCargo());
        get.put("shop", new GetShop());
        get.put("storage", new GetStorage());

        getall.put("customer_account", new GetAllCustomerAccounts());
        getall.put("cargo", new GetAllCargos());
        getall.put("shop", new GetAllShops());
        getall.put("storage", new GetAllStorages());
        getall.put("tariff", new GetAllTariffs());

        addto.put("shop", new AddToShop());
        addto.put("storage", new AddToStorage());
        Command help = new Help();

        boolean run_flag = true;

        while (run_flag) {
            try {
                System.out.print(">>>");
                str = br.readLine();
                if (str == null)
                    break;
                String[] words = str.split(" ");
                if (words.length == 0)
                    continue;
                if (words.length == 1) {
                    if (words[0].equals("exit"))
                        run_flag = false;
                    else if (words[0].equals("help"))
                        help.execute(null);
                    else
                        System.out.println("Incorrect input. Try 'help' for help");
                } else {
                    if (words[0].equals("create")) {
                        create.get(words[1]).execute(words);
                    } else if (words[0].equals("update")) {
                        update.get(words[1]).execute(words);
                    } else if (words[0].equals("get")) {
                        get.get(words[1]).execute(words);
                    } else if (words[0].equals("getall")) {
                        getall.get(words[1]).execute(words);
                    } else if (words[0].equals("addto")) {
                        addto.get(words[1]).execute(words);
                    }
                }
            } catch ( NullPointerException e ) {
                System.err.println("Incorrect argument for command. Try 'help' for help");
            } catch ( NumberFormatException e ) {
                System.err.println("Incorrect argument for command. Try 'help' for help");
            } catch ( IllegalArgumentException e ) {
                System.err.println("Incorrect argument for command " + e + ". Try 'help' for help");
            } catch( IOException e ) {
                System.err.println(e);
            }
        }
    }
}
